package org.qh.common.access.config;

import org.qh.common.access.constants.AccessConstants;
import org.qh.common.access.enums.AccountType;
import org.qh.common.access.processor.MiniProgramAccessHandler;
import org.qh.common.access.processor.MiniProgramLogHandler;
import org.qh.common.access.processor.MiniProgramProcessor;
import org.qh.common.access.properties.MinProgramProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;


/**
 * <p>Description: 微信小程序后配置 </p>
 */
@AutoConfiguration
@EnableConfigurationProperties(MinProgramProperties.class)  // AccessConstants.ITEM_MINI_PROGRAM_ENABLED
public class MinProgramConfiguration {

    private static final Logger log = LoggerFactory.getLogger(MinProgramConfiguration.class);

    @Bean(name = "miniProgramProcessor")
    @ConditionalOnProperty(prefix = AccessConstants.PROPERTY_ACCESS_MINI_PROGRAM, name = "enabled", havingValue = "true")
    public MiniProgramProcessor miniProgramProcessor(MinProgramProperties minProgramProperties) {
        MiniProgramProcessor miniProgramProcessor = new MiniProgramProcessor();
        miniProgramProcessor.setMinProgramProperties(minProgramProperties);
        miniProgramProcessor.setMiniProgramLogHandler(new MiniProgramLogHandler());
        log.trace("Bean [MinProgram Processor] Auto Configure.");
        return miniProgramProcessor;
    }

    @Bean(AccountType.WECHAT_MINI_PROGRAM_HANDLER)
    @ConditionalOnBean(MiniProgramProcessor.class)
    @ConditionalOnProperty(prefix = AccessConstants.PROPERTY_ACCESS_MINI_PROGRAM, name = "enabled", havingValue = "true")
    public MiniProgramAccessHandler miniProgramAccessHandler(MiniProgramProcessor miniProgramProcessor) {
        MiniProgramAccessHandler miniProgramAccessHandler = new MiniProgramAccessHandler(miniProgramProcessor);
        log.debug("Bean [MinProgram Access Handler] Auto Configure.");
        return miniProgramAccessHandler;
    }
}
